<?php

/**
 *   OPENSHOP 管理中心条码方案
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: barcode_scheme.php,v 1.0 2009/06/17 08:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');
//$exc = new exchange($os->table("barcode_scheme"), $db, 'scheme_id');

/*------------------------------------------------------ */
//-- 条码识别方案列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'identify')
{	
	//检查权限
	admin_priv();
		
    $smarty->assign('ur_here',      '条码识别方案管理');
    $smarty->assign('action_link',  array('text' => '添加条码识别方案', 'href' => 'barcode_scheme.php?act=add_identify'));
    $smarty->assign('full_page',    1);

    $scheme_list = get_bcs_identify_list();

    $smarty->assign('scheme_list',  $scheme_list['scheme']);
    $smarty->assign('filter',       $scheme_list['filter']);
    $smarty->assign('record_count', $scheme_list['record_count']);
    $smarty->assign('page_count',   $scheme_list['page_count']);
    
    assign_query_info();
    
    $smarty->display('bcs_identify_list.tpl');
}
/*------------------------------------------------------ */
//-- 条码识别方案翻页，排序
/*------------------------------------------------------ */
elseif($_REQUEST['act']=='query_identify'){
	
    $list = get_bcs_identify_list();
    $smarty->assign('scheme_list',          $list['scheme']);
    $smarty->assign('filter',         	    $list['filter']);
    $smarty->assign('record_count',   		$list['record_count']);
    $smarty->assign('page_count',     		$list['page_count']);

    make_json_result($smarty->fetch('bcs_identify_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));  
}
/*------------------------------------------------------ */
//-- 添加、编辑条码识别方案
/*------------------------------------------------------ */
elseif($_REQUEST['act']=='add_identify' || $_REQUEST['act'] == 'edit_identify'){
	
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add_identify';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_bcs_identify' : 'edit_bcs_identify';
    admin_priv($priv_str);
    
    $smarty->assign('form_action', $is_add ? 'insert_identify' : 'update_identify');

    /* 初始化、取得条码识别方案 */
    if ($is_add)
    {
        $scheme = array(
            'scheme_id'              	=> 0,
            'mode'   				    => 1,
            'length'         	        => '',
            'separate_sign'             => '',
            'refer1'                    => 1,
            'length1'         	        => '',
            'refer2'                    => 2,
            'length2'         	        => '',
            'refer3'                    => 3,
            'length3'         	        => '',
            'refer4'                    => 4,
            'length4'         	        => '',
            'refer5'                    => 1,
            'refer5_rand_length'        => ''     
        );
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }
        $id = intval($_GET['id']);
        $sql = "SELECT * FROM " . $os->table('barcode_scheme') . " WHERE scheme_id='$id'";
        $scheme = $db->GetRow($sql); 
           
        if (empty($scheme))
        {
            sys_msg('条码识别方案不存在');
        }
    }    
    $scheme['refer_range']          =  array(0=>'不启用', 1=>'类目', 2=>'商品', 3=>'颜色', 4=>'尺码');
    $smarty->assign('scheme', $scheme);

    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', '添加条码识别方案');
    }
    else
    {
        $smarty->assign('ur_here', '编辑条码识别方案');
    }
    if ($is_add)
    {
        $href = 'barcode_scheme.php?act=identify';
    }
    else
    {
        $href = 'barcode_scheme.php?act=identify&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => '条码识别方案管理'));
    
    assign_query_info();
    
    $smarty->display('bcs_identify_info.tpl');  
}
/*------------------------------------------------------ */
//-- 提交添加、编辑条码识别方案
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'insert_identify' || $_REQUEST['act'] == 'update_identify')
{
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert_identify';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_bcs_identify' : 'edit_bcs_identify';
    admin_priv($priv_str);

    /* 提交值 */
    $scheme = array(
        'scheme_id'     			        => intval($_POST['id']),    
		'mode'   			        	    => intval($_POST['mode']),
		'length'      				        => ($_POST['mode']!=1) ? 0 : intval($_POST['length']),
		'separate_sign'         		    => sub_str($_POST['separate_sign'], 255, false),
		'refer1'         				    => intval($_POST['refer1']),
		'length1'         				    => intval($_POST['length1']),
		'refer2'         				    => intval($_POST['refer2']),
		'length2'         				    => intval($_POST['length2']),
		'refer3'         				    => intval($_POST['refer3']),
		'length3'         				    => intval($_POST['length3']),
		'refer4'         				    => intval($_POST['refer4']),
		'length4'         				    => intval($_POST['length4']),
		'refer5'         				    => intval($_POST['refer5']),
		'refer5_rand_length'			    => ($_POST['mode']==1 || $_POST['refer5']!=1) ? 0 : intval($_POST['refer5_rand_length'])
    );
    
    /* 保存条码识别方案信息 */
    if ($is_add)
    {
        $db->autoExecute($os->table('barcode_scheme'), $scheme, 'INSERT');
        $scheme['scheme_id'] = $db->insert_id();
    }
    else
    {
        $db->autoExecute($os->table('barcode_scheme'), $scheme, 'UPDATE', "scheme_id = '$scheme[scheme_id]'");
    }

    /* 记日志 */
    if ($is_add)
    {
        admin_log('条码识别方案, ID号 ' . $scheme['scheme_id'], 'add', 'bcs_identify');
    }
    else
    {
        admin_log('条码识别方案, ID号 ' . $scheme['scheme_id'], 'edit', 'bcs_identify');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'barcode_scheme.php?act=add_identify', 'text' => '继续添加新的条码识别方案'),
            array('href' => 'barcode_scheme.php?act=identify', 'text' => '返回条码识别方案列表')
        );
        sys_msg('添加条码识别方案成功', 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'barcode_scheme.php?act=identify&' . list_link_postfix(), 'text' => '返回条码识别方案列表')
        );
        sys_msg('编辑条码识别方案成功', 0, $links);
    }
}
/*------------------------------------------------------ */
//-- 删除条码识别方案
/*------------------------------------------------------ */
elseif($_REQUEST['act']=='remove_identify'){
	
    check_authz_json('drop_bcs_identify');

    $scheme_id   = intval($_GET['id']);
    $scheme = $db->getRow("SELECT scheme_id FROM " . $os->table('barcode_scheme') . " WHERE scheme_id='$scheme_id'"); 
    if(empty($scheme))
    {
        make_json_error('条码识别方案不存在');      	
    }
    if($db->query("DELETE FROM " . $os->table('barcode_scheme') . " WHERE scheme_id='$scheme_id'")){
	    /* 记日志 */
	    admin_log("删除条码识别方案，ID:$scheme_id", 'remove', 'bcs_identify');
    }

    /* 清除缓存 */
    clear_cache_files();
     
    $url = 'barcode_scheme.php?act=query_identify&' . str_replace('act=remove_identify', '', $_SERVER['QUERY_STRING']);
    os_header("Location: $url\n");
    exit;	
}
/*------------------------------------------------------ */
//-- 条码生成方案列表
/*------------------------------------------------------ */
elseif($_REQUEST['act'] == 'generate'){
	//检查权限
	admin_priv();
		
    $smarty->assign('ur_here',      '条码生成方案管理');
    $smarty->assign('action_link',  array('text' => '添加条码生成方案', 'href' => 'barcode_scheme.php?act=add_generate'));
    $smarty->assign('full_page',    1);

    $scheme_list = get_bcs_generate_list();

    $smarty->assign('scheme_list',  $scheme_list['scheme']);
    $smarty->assign('filter',       $scheme_list['filter']);
    $smarty->assign('record_count', $scheme_list['record_count']);
    $smarty->assign('page_count',   $scheme_list['page_count']);
    
    assign_query_info();
    
    $smarty->display('bcs_generate_list.tpl');	
}
/*------------------------------------------------------ */
//-- 条码生成方案翻页，排序
/*------------------------------------------------------ */
elseif($_REQUEST['act']=='query_generate'){
	
    $list = get_bcs_generate_list();
    $smarty->assign('scheme_list',          $list['scheme']);
    $smarty->assign('filter',         	    $list['filter']);
    $smarty->assign('record_count',   		$list['record_count']);
    $smarty->assign('page_count',     		$list['page_count']);

    make_json_result($smarty->fetch('bcs_generate_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));  
}
/*------------------------------------------------------ */
//-- 添加、编辑条码生成方案
/*------------------------------------------------------ */
elseif($_REQUEST['act']=='add_generate' || $_REQUEST['act'] == 'edit_generate'){
	
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add_generate';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_bcs_generate' : 'edit_bcs_generate';
    admin_priv($priv_str);
    
    $smarty->assign('form_action', $is_add ? 'insert_generate' : 'update_generate');

    /* 初始化、取得条码生成方案 */
    if ($is_add)
    {
        $scheme = array(
            'scheme_id'              	=> 0,
            'mode'   				    => 1,
            'length'         	        => '',
            'separate_sign'             => '',
            'refer1'                    => 1,
            'refer2'                    => 2,
            'refer3'                    => 3,
            'refer4'                    => 4,
            'refer5'                    => 1,
            'refer5_rand_length'        => ''     
        );
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }
        $id = intval($_GET['id']);
        $sql = "SELECT * FROM " . $os->table('barcode_generate_scheme') . " WHERE scheme_id='$id'";
        $scheme = $db->GetRow($sql); 
           
        if (empty($scheme))
        {
            sys_msg('条码生成方案不存在');
        }
    }    
    $scheme['refer_range']          =  array(0=>'不启用', 1=>'类目', 2=>'商品', 3=>'颜色', 4=>'尺码');
    $smarty->assign('scheme', $scheme);

    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', '添加条码生成方案');
    }
    else
    {
        $smarty->assign('ur_here', '编辑条码生成方案');
    }
    if ($is_add)
    {
        $href = 'barcode_scheme.php?act=generate';
    }
    else
    {
        $href = 'barcode_scheme.php?act=generate&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => '条码生成方案管理'));
    
    assign_query_info();
    
    $smarty->display('bcs_generate_info.tpl');  
}
/*------------------------------------------------------ */
//-- 提交添加、编辑条码生成方案
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'insert_generate' || $_REQUEST['act'] == 'update_generate')
{
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert_generate';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_bcs_generate' : 'edit_bcs_generate';
    admin_priv($priv_str);

    /* 提交值 */
    $scheme = array(
        'scheme_id'     			        => intval($_POST['id']),    
		'mode'   			        	    => intval($_POST['mode']),
		'length'      				        => ($_POST['mode']!=1) ? 0 : intval($_POST['length']),
		'separate_sign'         		    => sub_str($_POST['separate_sign'], 255, false),
		'refer1'         				    => intval($_POST['refer1']),
		'refer2'         				    => intval($_POST['refer2']),
		'refer3'         				    => intval($_POST['refer3']),
		'refer4'         				    => intval($_POST['refer4']),
		'refer5'         				    => intval($_POST['refer5']),
		'refer5_rand_length'			    => ($_POST['mode']==1 || $_POST['refer5']!=1) ? 0 : intval($_POST['refer5_rand_length'])
    );
    /* 保存条码识别方案信息 */
    if ($is_add)
    {
        $db->autoExecute($os->table('barcode_generate_scheme'), $scheme, 'INSERT');
        $scheme['scheme_id'] = $db->insert_id();
    }
    else
    {
        $db->autoExecute($os->table('barcode_generate_scheme'), $scheme, 'UPDATE', "scheme_id = '$scheme[scheme_id]'");
    }

    /* 记日志 */
    if ($is_add)
    {
        admin_log('条码生成方案, ID号 ' . $scheme['scheme_id'], 'add', 'bcs_generate');
    }
    else
    {
        admin_log('条码生成方案, ID号 ' . $scheme['scheme_id'], 'edit', 'bcs_generate');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'barcode_scheme.php?act=add_generate', 'text' => '继续添加新的条码生成方案'),
            array('href' => 'barcode_scheme.php?act=generate', 'text' => '返回条码生成方案列表')
        );
        sys_msg('添加条码生成方案成功', 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'barcode_scheme.php?act=generate&' . list_link_postfix(), 'text' => '返回条码生成方案列表')
        );
        sys_msg('编辑条码生成方案成功', 0, $links);
    }
}
/*------------------------------------------------------ */
//-- 删除条码生成方案
/*------------------------------------------------------ */
elseif($_REQUEST['act']=='remove_generate'){
	
    check_authz_json('drop_bcs_generate');

    $scheme_id   = intval($_GET['id']);
    $scheme = $db->getRow("SELECT scheme_id FROM " . $os->table('barcode_generate_scheme') . " WHERE scheme_id='$scheme_id'"); 
    if(empty($scheme))
    {
        make_json_error('条码生成方案不存在');      	
    }
    if($db->query("DELETE FROM " . $os->table('barcode_generate_scheme') . " WHERE scheme_id='$scheme_id'")){
	    /* 记日志 */
	    admin_log("删除条码生成方案，ID:$scheme_id", 'remove', 'bcs_generate');
    }

    /* 清除缓存 */
    clear_cache_files();
     
    $url = 'barcode_scheme.php?act=query_generate&' . str_replace('act=remove_generate', '', $_SERVER['QUERY_STRING']);
    os_header("Location: $url\n");
    exit;	
}

function get_bcs_identify_list(){
   $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'scheme_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        
        $where = 'WHERE 1 ';
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('barcode_generate_scheme') . $where;

        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT * FROM " . $GLOBALS['os']->table('barcode_scheme') . $where . " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {   
    	$refer_range   = array(0=>'不启用', 1=>'类目', 2=>'商品', 3=>'颜色', 4=>'尺码');
        $row['refer1'] = $refer_range[$row['refer1']];
        $row['refer2'] = $refer_range[$row['refer2']];
        $row['refer3'] = $refer_range[$row['refer3']];
        $row['refer4'] = $refer_range[$row['refer4']];
        $arr[] = $row;
    }
    return array('scheme' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

function get_bcs_generate_list(){
   $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'scheme_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        
        $where = 'WHERE 1 ';
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('barcode_generate_scheme') . $where;

        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT * FROM " . $GLOBALS['os']->table('barcode_generate_scheme') . $where . " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {   
    	$refer_range   = array(0=>'不启用', 1=>'类目', 2=>'商品', 3=>'颜色', 4=>'尺码');
        $row['refer1'] = $refer_range[$row['refer1']];
        $row['refer2'] = $refer_range[$row['refer2']];
        $row['refer3'] = $refer_range[$row['refer3']];
        $row['refer4'] = $refer_range[$row['refer4']];
        $arr[] = $row;
    }
    return array('scheme' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);		
}
?>